const fs = require('fs')
const path = require('path')

function parse (config) {
  const lines = config.split(/\r?\n/)
  const result = {}
  lines.forEach((line) => {
    if (/^\s*$/m.test(line)) return // ignore blank line
    if (line.search('=')) {
      const kv = line.split('=')
      result[kv[0]] = kv[1]
    }
  })
  return result
}

module.exports = function parseConfig (relativePath) {
  return Promise.resolve({
    then: (resolve, reject) => {
      fs.readFile(path.resolve(__dirname, relativePath), 'utf-8', (err, data) => {
        if (err) reject(err)
        if (!data) reject(new Error('无法获取配置文件数据'))
        const result = parse(data)
        resolve(result)
      })
    }
  })
}